package net.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * 	聊天的客户端
 * @author Administrator
 *
 */
public class UDPChatClient {
	
	
	public static void main(String[] args) throws SocketException, IOException {
		
		// 创建socket
		DatagramSocket socket = new DatagramSocket();
		
		// 准备数据
		Scanner scanner = new Scanner(System.in);
		
		// 循环写数据，当输入bye的时候结束
		InetAddress address = InetAddress.getByName("localhost");
		while(true) {
			String in = scanner.nextLine();
			
			// 当输入的是bye的时候结束
			if("bye".equals(in)) {
				break;
			}
			
			// 构建并输出数据
			byte[] bytes = in.getBytes();
			DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, address, 8888);
			
			// 发送数据
			socket.send(packet);
		}
		
		// 关闭资源
		scanner.close();
		socket.close();
		
	}

}
